using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Grafana.Loki;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Tiens.NotificationHub.WebHost
{
    public class Program
    {
        public static int Main(string[] args)
        {
            var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environments.Development;
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true)
                .AddJsonFile($"appsettings.{currentEnv}.json", optional: true)
                .AddEnvironmentVariables()
                .AddCommandLine(args)
                .Build();

            var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;

            Log.Logger = new LoggerConfiguration()
#if DEBUG
                .MinimumLevel.Debug()
#endif
                //.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                //.MinimumLevel.Override("Tiens.NotificationHub.Core.Consumers", LogEventLevel.Information)
                .Enrich.FromLogContext()
                .WriteTo.GrafanaLoki(config["Loki:Url"], labels: new List<LokiLabel>() { new LokiLabel() { Key = "app", Value = assemblyName + "_" + currentEnv } })
#if DEBUG
                .WriteTo.Console()
#endif
                .CreateLogger();

            try
            {
                Log.Information("Starting Tiens.FinancialVoucherManagement.WebHost.");
                CreateHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "WebHost terminated unexpectedly!");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).UseSerilog((hostCtx, logCfg) =>
                {
                    var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;
                    var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environments.Development;
                    var config = hostCtx.Configuration;

                    logCfg
                    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                    .MinimumLevel.Override("Tiens.NotificationHub.Core.Consumers", LogEventLevel.Information)
                    .Enrich.FromLogContext()
                    .WriteTo.GrafanaLoki(config["Loki:Url"], labels: new List<LokiLabel>() { new LokiLabel() { Key = "app", Value = assemblyName + "_" + currentEnv } });
                });

    }
}
